/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.luolc.emojirain.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;

import com.chinasoft_ohos.commontools.toast.Toast;
import com.luolc.emojirain.EmojiRainLayout;
import com.luolc.emojirain.sample.ResourceTable;

import java.util.stream.Stream;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-28
 */
public class MainAbilitySlice extends AbilitySlice {
    EmojiRainLayout mContainer;
    TextField mPer;
    TextField mDuration;
    TextField mDropDuration;
    TextField mFrequency;
    Button button;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mContainer = (EmojiRainLayout) findComponentById(ResourceTable.Id_group_emoji_container);
        mPer = (TextField) findComponentById(ResourceTable.Id_et_per);
        mDuration = (TextField) findComponentById(ResourceTable.Id_et_duration);
        mDropDuration = (TextField) findComponentById(ResourceTable.Id_et_drop_duration);
        mFrequency = (TextField) findComponentById(ResourceTable.Id_et_frequency);
        button = (Button) findComponentById(ResourceTable.Id_btn_drop);
        mContainer.addEmoji(ResourceTable.Media_emoji_1_3);
        mContainer.addEmoji(ResourceTable.Media_emoji_2_3);
        mContainer.addEmoji(ResourceTable.Media_emoji_3_3);
        mContainer.addEmoji(ResourceTable.Media_emoji_4_3);
        mContainer.addEmoji(ResourceTable.Media_emoji_5_3);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!checkInputParams(mPer, mDuration, mDropDuration, mFrequency)) {
                    Toast.show(getString(ResourceTable.String_please_input_all_params));
                    return;
                }
                mContainer.stopDropping();
                try {
                    int per = Integer.parseInt(mPer.getText().toString());
                    int duration = Integer.parseInt(mDuration.getText().toString());
                    int dropDuration = Integer.parseInt(mDropDuration.getText().toString());
                    int frequency = Integer.parseInt(mFrequency.getText().toString());
                    mContainer.setPer(per);
                    mContainer.setDuration(duration);
                    mContainer.setDropDuration(dropDuration);
                    mContainer.setDropFrequency(frequency);
                    mContainer.startDropping();
                } catch (Exception exception) {
                    Toast.show(getContext().getString(com.luolc.emojirain.ResourceTable.String_toast_number));
                }
            }
        });
        if (!mPer.isFocused()) {
            mPer.requestFocus();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private boolean checkInputParams(TextField... params) {
        return Stream.of(params).allMatch(param -> param.getText().length() != 0);
    }
}
